﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class PutawayRuleDetailDA : IPutawayRuleDetailDA
    {
        private string CREATED_BY = "@CREATED_BY";
        private string DIM_RESTRICTION01 = "@DIM_RESTRICTION01";
        private string DIM_RESTRICTION02 = "@DIM_RESTRICTION02";
        private string DIM_RESTRICTION03 = "@DIM_RESTRICTION03";
        private string DIM_RESTRICTION04 = "@DIM_RESTRICTION04";
        private string DIM_RESTRICTION05 = "@DIM_RESTRICTION05";
        private string DIM_RESTRICTION06 = "@DIM_RESTRICTION06";
        private string FROM_LOC = "@FROM_LOC";
        private string IS_ACTIVE = "@IS_ACTIVE";
        private string LINE_ID = "@LINE_ID";
        private string LOC_SELECTION = "@LOC_SELECTION";
        private string LOC_STATE_RESTRICTION01 = "@LOC_STATE_RESTRICTION01";
        private string LOC_STATE_RESTRICTION02 = "@LOC_STATE_RESTRICTION02";
        private string LOC_STATE_RESTRICTION03 = "@LOC_STATE_RESTRICTION03";
        private string LOC_STATE_RESTRICTION04 = "@LOC_STATE_RESTRICTION04";
        private string LOC_STATE_RESTRICTION05 = "@LOC_STATE_RESTRICTION05";
        private string PA_STRATEGY = "@PA_STRATEGY";
        private string PUTAWAY_RULE_ID = "@PUTAWAY_RULE_ID";
        private string SQL_CHECK_PUTAWAY_RULE_DETAIL_ID_UNIQUE = " SELECT COUNT(1) FROM @_@PUTAWAY_RULE_DETAIL WHERE PUTAWAY_RULE_ID = @PUTAWAY_RULE_ID AND LINE_ID = @LINE_ID ";
        private string SQL_DELETE_ONE_PUTAWAY_RULE_DETAIL = " DELETE FROM @_@PUTAWAY_RULE_DETAIL WHERE  PUTAWAY_RULE_ID = @PUTAWAY_RULE_ID AND LINE_ID = @LINE_ID ";
        private string SQL_DELETE_PUTAWAY_RULE_DETAIL = " DELETE FROM @_@PUTAWAY_RULE_DETAIL WHERE  PUTAWAY_RULE_ID = @PUTAWAY_RULE_ID ";
        private string SQL_INSERT_PUTAWAY_RULE_DETAIL = " INSERT INTO @_@PUTAWAY_RULE_DETAIL ( WH_ID, PUTAWAY_RULE_ID, LINE_ID, PA_STRATEGY, FROM_LOC, TO_LOC, ZONE, DIM_RESTRICTION01, DIM_RESTRICTION02, DIM_RESTRICTION03, DIM_RESTRICTION04, DIM_RESTRICTION05, DIM_RESTRICTION06, LOC_STATE_RESTRICTION01, LOC_STATE_RESTRICTION02, LOC_STATE_RESTRICTION03, LOC_STATE_RESTRICTION04, LOC_STATE_RESTRICTION05, LOC_SELECTION, IS_ACTIVE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @PUTAWAY_RULE_ID, @LINE_ID, @PA_STRATEGY, @FROM_LOC, @TO_LOC, @ZONE, @DIM_RESTRICTION01, @DIM_RESTRICTION02, @DIM_RESTRICTION03, @DIM_RESTRICTION04, @DIM_RESTRICTION05, @DIM_RESTRICTION06, @LOC_STATE_RESTRICTION01, @LOC_STATE_RESTRICTION02, @LOC_STATE_RESTRICTION03, @LOC_STATE_RESTRICTION04, @LOC_STATE_RESTRICTION05, @LOC_SELECTION, @IS_ACTIVE, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_PUTAWAY_RULE_DETAIL = " SELECT A.ROWID,A.WH_ID,A.PUTAWAY_RULE_ID,A.LINE_ID,A.PA_STRATEGY,A.FROM_LOC,A.TO_LOC,A.ZONE,A.DIM_RESTRICTION01,A.DIM_RESTRICTION02,A.DIM_RESTRICTION03,A.DIM_RESTRICTION04,A.DIM_RESTRICTION05,A.DIM_RESTRICTION06,A.LOC_STATE_RESTRICTION01,A.LOC_STATE_RESTRICTION02,A.LOC_STATE_RESTRICTION03,A.LOC_STATE_RESTRICTION04,A.LOC_STATE_RESTRICTION05,A.LOC_SELECTION,A.IS_ACTIVE,A.TS,A.CREATED_BY,A.CREATED_DATE,A.UPDATED_BY,A.UPDATED_DATE ,T.[TEXT] AS DESCRIPTION  FROM  @_@PUTAWAY_RULE_DETAIL A  LEFT JOIN @_@SYS_CODE_DETAIL B ON A.PA_STRATEGY = B.SYS_CODE AND B.SYS_CODE_TYPE = 'PTSTRATEGIES' LEFT JOIN sysadm.TRANSLATION T ON 'PTSTRATEGIES.'+A.PA_STRATEGY = T.ITEM_ID  \t\t\tAND  T.ITEM_TYPE = 'SYS_CODE_DETAIL' AND T.LANG+'-'+T.COUNTRY = @LANGUAGE ";
        private string SQL_UPDATE_PUTAWAY_RULE_DETAIL = " UPDATE @_@PUTAWAY_RULE_DETAIL SET WH_ID = @WH_ID, PA_STRATEGY = @PA_STRATEGY, FROM_LOC = @FROM_LOC, TO_LOC = @TO_LOC, ZONE = @ZONE, DIM_RESTRICTION01 = @DIM_RESTRICTION01, DIM_RESTRICTION02 = @DIM_RESTRICTION02, DIM_RESTRICTION03 = @DIM_RESTRICTION03, DIM_RESTRICTION04 = @DIM_RESTRICTION04, DIM_RESTRICTION05 = @DIM_RESTRICTION05, DIM_RESTRICTION06 = @DIM_RESTRICTION06, LOC_STATE_RESTRICTION01 = @LOC_STATE_RESTRICTION01, LOC_STATE_RESTRICTION02 = @LOC_STATE_RESTRICTION02, LOC_STATE_RESTRICTION03 = @LOC_STATE_RESTRICTION03, LOC_STATE_RESTRICTION04 = @LOC_STATE_RESTRICTION04, LOC_STATE_RESTRICTION05 = @LOC_STATE_RESTRICTION05, LOC_SELECTION = @LOC_SELECTION, IS_ACTIVE = @IS_ACTIVE, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE PUTAWAY_RULE_ID = @PUTAWAY_RULE_ID AND LINE_ID = @LINE_ID  ";
        private string TO_LOC = "@TO_LOC";
        private string TS = "@TS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";
        private string ZONE = "@ZONE";

        public bool CheckPutawayRuleDetailIDUnique(string putawayRuleID, string LineID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PUTAWAY_RULE_ID", putawayRuleID), new SqlParameter("@LINE_ID", LineID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_PUTAWAY_RULE_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckPutawayRuleDetailIDUnique(DataBase dataBase, DbTransaction tran, string putawayRuleID, string lineID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PUTAWAY_RULE_ID", putawayRuleID), new SqlParameter("@LINE_ID", lineID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_PUTAWAY_RULE_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeletePutawayRuleDetail(List<string> putawayRuleID, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    string sqlStr = this.SQL_DELETE_PUTAWAY_RULE_DETAIL.Replace("@_@", whLoginID[i]);
                    for (int j = 0; j < putawayRuleID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PUTAWAY_RULE_ID", putawayRuleID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, sqlStr, paras);
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeletePutawayRuleDetail(List<string> putawayRuleID, List<string> lineID, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    string sqlStr = this.SQL_DELETE_ONE_PUTAWAY_RULE_DETAIL.Replace("@_@", whLoginID[i]);
                    for (int j = 0; j < putawayRuleID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PUTAWAY_RULE_ID", putawayRuleID[j]), new SqlParameter("@LINE_ID", lineID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, sqlStr, paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeletePutawayRuleDetail(DataBase dataBase, DbTransaction tran, List<string> putawayRuleID, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    string sqlStr = this.SQL_DELETE_PUTAWAY_RULE_DETAIL.Replace("@_@", whLoginID[i]);
                    for (int j = 0; j < putawayRuleID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PUTAWAY_RULE_ID", putawayRuleID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, sqlStr, paras);
                    }
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return result;
        }

        public int DeletePutawayRuleDetail(DataBase dataBase, DbTransaction tran, List<string> putawayRuleID, List<string> lineID, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    string sqlStr = this.SQL_DELETE_ONE_PUTAWAY_RULE_DETAIL.Replace("@_@", whLoginID[i]);
                    for (int j = 0; j < putawayRuleID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PUTAWAY_RULE_ID", putawayRuleID[j]), new SqlParameter("@LINE_ID", lineID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, sqlStr, paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllPutawayRuleDetail(string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_PUTAWAY_RULE_DETAIL.Replace("@_@", whLoginID);
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            return DBHelper.ExecuteDataSet(CommandType.Text, sql, paras);
        }

        public DataSet GetAllPutawayRuleDetail(DataBase dataBase, DbTransaction tran, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_PUTAWAY_RULE_DETAIL.Replace("@_@", whLoginID);
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql, paras);
        }

        public PutawayRuleDetailInfo GetPutawayRuleDetailByID(string putawayRuleID, string lineID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_PUTAWAY_RULE_DETAIL.Replace("@_@", whLoginID) + " WHERE PUTAWAY_RULE_ID = @PUTAWAY_RULE_ID AND LINE_ID = @LINE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PUTAWAY_RULE_ID", putawayRuleID), new SqlParameter("@LINE_ID", lineID), new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            PutawayRuleDetailInfo putawayRuleDetailInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    putawayRuleDetailInfo = new PutawayRuleDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["PUTAWAY_RULE_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["PA_STRATEGY"].ToString(), reader["FROM_LOC"].ToString(), reader["TO_LOC"].ToString(), reader["ZONE"].ToString(), reader["DIM_RESTRICTION01"].ToString(), reader["DIM_RESTRICTION02"].ToString(), reader["DIM_RESTRICTION03"].ToString(), reader["DIM_RESTRICTION04"].ToString(), reader["DIM_RESTRICTION05"].ToString(), reader["DIM_RESTRICTION06"].ToString(), reader["LOC_STATE_RESTRICTION01"].ToString(), reader["LOC_STATE_RESTRICTION02"].ToString(), reader["LOC_STATE_RESTRICTION03"].ToString(), reader["LOC_STATE_RESTRICTION04"].ToString(), reader["LOC_STATE_RESTRICTION05"].ToString(), reader["LOC_SELECTION"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime(), reader["Description"].ToString());
                }
            }
            return putawayRuleDetailInfo;
        }

        public PutawayRuleDetailInfo GetPutawayRuleDetailByID(DataBase dataBase, DbTransaction tran, string putawayruledetailID, string lineID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_PUTAWAY_RULE_DETAIL.Replace("@_@", whLoginID) + " WHERE PUTAWAY_RULE_ID = @PUTAWAY_RULE_ID AND LINE_ID = @LINE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PUTAWAY_RULE_ID", putawayruledetailID), new SqlParameter("@LINE_ID", lineID), new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            PutawayRuleDetailInfo putawayRuleDetailInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                putawayRuleDetailInfo = new PutawayRuleDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["PUTAWAY_RULE_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["PA_STRATEGY"].ToString(), reader["FROM_LOC"].ToString(), reader["TO_LOC"].ToString(), reader["ZONE"].ToString(), reader["DIM_RESTRICTION01"].ToString(), reader["DIM_RESTRICTION02"].ToString(), reader["DIM_RESTRICTION03"].ToString(), reader["DIM_RESTRICTION04"].ToString(), reader["DIM_RESTRICTION05"].ToString(), reader["DIM_RESTRICTION06"].ToString(), reader["LOC_STATE_RESTRICTION01"].ToString(), reader["LOC_STATE_RESTRICTION02"].ToString(), reader["LOC_STATE_RESTRICTION03"].ToString(), reader["LOC_STATE_RESTRICTION04"].ToString(), reader["LOC_STATE_RESTRICTION05"].ToString(), reader["LOC_SELECTION"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime(), reader["Description"].ToString());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return putawayRuleDetailInfo;
        }

        public DataSet GetPutawayRuleDetailByQueryList(List<string> sqlWhere, PutawayRuleDetailQueryEntity putawayruledetailQuery, string whLoginID, PubParasInfo pubParasInfo)
        {
            string temp = this.SQL_SELECT_ALL_PUTAWAY_RULE_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!putawayruledetailQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, putawayruledetailQuery.CurrentPage, putawayruledetailQuery.PageSize, putawayruledetailQuery.SortField, putawayruledetailQuery.SortDirection).Replace("@LANGUAGE", "''" + pubParasInfo.UserLanguage + "''");
            }
            else
            {
                temp = temp.Replace("@LANGUAGE", "'" + pubParasInfo.UserLanguage + "'");
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetPutawayRuleDetailByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, PutawayRuleDetailQueryEntity putawayruledetailQuery, string whLoginID, PubParasInfo pubParasInfo)
        {
            string temp = this.SQL_SELECT_ALL_PUTAWAY_RULE_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!putawayruledetailQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, putawayruledetailQuery.CurrentPage, putawayruledetailQuery.PageSize, putawayruledetailQuery.SortField, putawayruledetailQuery.SortDirection).Replace("@LANGUAGE", "''" + pubParasInfo.UserLanguage + "''");
            }
            else
            {
                temp = temp.Replace("@LANGUAGE", "'" + pubParasInfo.UserLanguage + "'");
            }
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public List<PutawayRuleDetailInfo> GetPutawayRuleDetailListByID(string putawayRuleID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_PUTAWAY_RULE_DETAIL.Replace("@_@", whLoginID) + " WHERE A.PUTAWAY_RULE_ID = @PUTAWAY_RULE_ID  ORDER BY A.LINE_ID ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PUTAWAY_RULE_ID", putawayRuleID), new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            List<PutawayRuleDetailInfo> putawayRuleDetailInfos = new List<PutawayRuleDetailInfo>();
            PutawayRuleDetailInfo putaway = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                while (reader.Read())
                {
                    DateTime CS00002;
                    putaway = new PutawayRuleDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["PUTAWAY_RULE_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["PA_STRATEGY"].ToString(), reader["FROM_LOC"].ToString(), reader["TO_LOC"].ToString(), reader["ZONE"].ToString(), reader["DIM_RESTRICTION01"].ToString(), reader["DIM_RESTRICTION02"].ToString(), reader["DIM_RESTRICTION03"].ToString(), reader["DIM_RESTRICTION04"].ToString(), reader["DIM_RESTRICTION05"].ToString(), reader["DIM_RESTRICTION06"].ToString(), reader["LOC_STATE_RESTRICTION01"].ToString(), reader["LOC_STATE_RESTRICTION02"].ToString(), reader["LOC_STATE_RESTRICTION03"].ToString(), reader["LOC_STATE_RESTRICTION04"].ToString(), reader["LOC_STATE_RESTRICTION05"].ToString(), reader["LOC_SELECTION"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["Description"].ToString());
                    putawayRuleDetailInfos.Add(putaway);
                }
            }
            return putawayRuleDetailInfos;
        }

        public List<PutawayRuleDetailInfo> GetPutawayRuleDetailListByID(DataBase dataBase, DbTransaction tran, string putawayRuleID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_PUTAWAY_RULE_DETAIL.Replace("@_@", whLoginID) + " WHERE A.PUTAWAY_RULE_ID = @PUTAWAY_RULE_ID ORDER BY A.LINE_ID ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PUTAWAY_RULE_ID", putawayRuleID), new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            List<PutawayRuleDetailInfo> putawayRuleDetailInfos = new List<PutawayRuleDetailInfo>();
            PutawayRuleDetailInfo putaway = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            while (reader.Read())
            {
                DateTime CS00002;
                putaway = new PutawayRuleDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["PUTAWAY_RULE_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["PA_STRATEGY"].ToString(), reader["FROM_LOC"].ToString(), reader["TO_LOC"].ToString(), reader["ZONE"].ToString(), reader["DIM_RESTRICTION01"].ToString(), reader["DIM_RESTRICTION02"].ToString(), reader["DIM_RESTRICTION03"].ToString(), reader["DIM_RESTRICTION04"].ToString(), reader["DIM_RESTRICTION05"].ToString(), reader["DIM_RESTRICTION06"].ToString(), reader["LOC_STATE_RESTRICTION01"].ToString(), reader["LOC_STATE_RESTRICTION02"].ToString(), reader["LOC_STATE_RESTRICTION03"].ToString(), reader["LOC_STATE_RESTRICTION04"].ToString(), reader["LOC_STATE_RESTRICTION05"].ToString(), reader["LOC_SELECTION"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["Description"].ToString());
                putawayRuleDetailInfos.Add(putaway);
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return putawayRuleDetailInfos;
        }

        public int InsertPutawayRuleDetail(PutawayRuleDetailInfo putawayRuleDetailInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    putawayRuleDetailInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_PutawayRuleDetail_Parameters(putawayRuleDetailInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_PUTAWAY_RULE_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertPutawayRuleDetail(DataBase dataBase, DbTransaction tran, PutawayRuleDetailInfo putawayRuleDetailInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    putawayRuleDetailInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_PutawayRuleDetail_Parameters(putawayRuleDetailInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_PUTAWAY_RULE_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_PutawayRuleDetail_Parameters(PutawayRuleDetailInfo putawayRuleDetailInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { 
                new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.PUTAWAY_RULE_ID, SqlDbType.VarChar, 20), new SqlParameter(this.LINE_ID, SqlDbType.Int, 4), new SqlParameter(this.PA_STRATEGY, SqlDbType.VarChar, 20), new SqlParameter(this.FROM_LOC, SqlDbType.VarChar, 10), new SqlParameter(this.TO_LOC, SqlDbType.VarChar, 10), new SqlParameter(this.ZONE, SqlDbType.VarChar, 10), new SqlParameter(this.DIM_RESTRICTION01, SqlDbType.VarChar, 20), new SqlParameter(this.DIM_RESTRICTION02, SqlDbType.VarChar, 20), new SqlParameter(this.DIM_RESTRICTION03, SqlDbType.VarChar, 20), new SqlParameter(this.DIM_RESTRICTION04, SqlDbType.VarChar, 20), new SqlParameter(this.DIM_RESTRICTION05, SqlDbType.VarChar, 20), new SqlParameter(this.DIM_RESTRICTION06, SqlDbType.VarChar, 20), new SqlParameter(this.LOC_STATE_RESTRICTION01, SqlDbType.VarChar, 20), new SqlParameter(this.LOC_STATE_RESTRICTION02, SqlDbType.VarChar, 20), new SqlParameter(this.LOC_STATE_RESTRICTION03, SqlDbType.VarChar, 20), 
                new SqlParameter(this.LOC_STATE_RESTRICTION04, SqlDbType.VarChar, 20), new SqlParameter(this.LOC_STATE_RESTRICTION05, SqlDbType.VarChar, 20), new SqlParameter(this.LOC_SELECTION, SqlDbType.VarChar, 20), new SqlParameter(this.IS_ACTIVE, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 0x12), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 0x12)
             };
            if (!string.IsNullOrEmpty(putawayRuleDetailInfo.WhID))
            {
                paramArray[0].Value = putawayRuleDetailInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = putawayRuleDetailInfo.PutawayRuleID;
            paramArray[2].Value = putawayRuleDetailInfo.LineID;
            paramArray[3].Value = putawayRuleDetailInfo.PaStrategy;
            paramArray[4].Value = putawayRuleDetailInfo.FromLoc;
            paramArray[5].Value = putawayRuleDetailInfo.ToLoc;
            paramArray[6].Value = putawayRuleDetailInfo.Zone;
            paramArray[7].Value = putawayRuleDetailInfo.DimRestriction01;
            paramArray[8].Value = putawayRuleDetailInfo.DimRestriction02;
            paramArray[9].Value = putawayRuleDetailInfo.DimRestriction03;
            paramArray[10].Value = putawayRuleDetailInfo.DimRestriction04;
            paramArray[11].Value = putawayRuleDetailInfo.DimRestriction05;
            paramArray[12].Value = putawayRuleDetailInfo.DimRestriction06;
            paramArray[13].Value = putawayRuleDetailInfo.LocStateRestriction01;
            paramArray[14].Value = putawayRuleDetailInfo.LocStateRestriction02;
            paramArray[15].Value = putawayRuleDetailInfo.LocStateRestriction03;
            paramArray[0x10].Value = putawayRuleDetailInfo.LocStateRestriction04;
            paramArray[0x11].Value = putawayRuleDetailInfo.LocStateRestriction05;
            if (!string.IsNullOrEmpty(putawayRuleDetailInfo.LocSelection))
            {
                paramArray[0x12].Value = putawayRuleDetailInfo.LocSelection;
            }
            else
            {
                paramArray[0x12].Value = DBNull.Value;
            }
            paramArray[0x13].Value = putawayRuleDetailInfo.IsActive;
            paramArray[20].Value = putawayRuleDetailInfo.CreatedBy;
            paramArray[0x15].Value = putawayRuleDetailInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdatePutawayRuleDetail(PutawayRuleDetailInfo putawayRuleDetailInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    putawayRuleDetailInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_PutawayRuleDetail_Parameters(putawayRuleDetailInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_PUTAWAY_RULE_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdatePutawayRuleDetail(DataBase dataBase, DbTransaction tran, PutawayRuleDetailInfo putawayRuleDetailInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    putawayRuleDetailInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_PutawayRuleDetail_Parameters(putawayRuleDetailInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_PUTAWAY_RULE_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

